﻿#SingleInstance force
#NoEnv
; #NoTrayIcon
SetBatchLines -1
DetectHiddenWindows Off
; DetectHiddenWindows On

; === CONFIGURATION ===
; Key := "Esc & `o"
; Key1 := "Capslock & o"
Key := "Esc & `;"
Key1 := "Capslock & `;"
PasteKey := "Esc & v"
PasteKey1 := "Capslock & v"
Width := 500
MaxItems := 10
LargeIcons := true
CenterText := false
MoveUp := 120
Color := "404040"
MyRed := "EE3F4D"
SelColor := "44C6F6"
Prefix := "."
AllowDupe := true
FadeTime := 50
IgnoreEquals := "^(NVIDIA GeForce Overlay|Program Manager|Settings)$"
PasteStrings := []
PasteStrings.Push({str: "hello >++> >++> world", tag: "hello"})
PasteStrings.Push({str: "find / -name "">++>""", tag: "linux"})
PasteStrings.Push({str: "ps -ef | grep "">++>""", tag: "linux"})
PasteStrings.Push({str: "基于电信业务的LRU增强算法研究", tag: "paper"})
PasteStrings.Push({str: "192.168.3.8", tag: "ceip"})
PasteStrings.Push({str: "192.168.3.9", tag:"elip"})
PasteStrings.Push({str: "192.168.3.7", tag: "miniip"})
PasteStrings.Push({str: "zzx@192.168.3.8:", tag: "cescp"})
PasteStrings.Push({str: "zzx@192.168.3.7", tag: "miniscp"})
PasteStrings.Push({str: "zzx@192.168.3.9", tag: "el"})
PasteSubstitution := ">++>"
; === END CONFIGURATION ===

global PasteSubstitution, PasteStrings, Key, Key1, PasteKey, PasteKey1, Width, MaxItems, Color, MoveUp, LargeIcons, FontSize, IgnoreEquals, Prefix, AllowDupe, FadeTime

; set up gui
Switcher := new Switcher("Window Switcher", "+ToolWindow -Caption +Border") ; removed ToolWindow to make RegisterWindowMessage work with this hwnd
Switcher.Margin(0, 0)
Switcher.Color(Color, Color)
; Switcher.Font("s11 cWhite")
Switcher.Font("s11 cWhite", "FiraCode Nerd Font")
Switcher.Edit := Switcher.Add("Edit", "w" Width " h26 -Border -Multi " (CenterText ? "Center" : ""),, Switcher.Input.Bind(Switcher))
Switcher.LV := new Gui.ListView(Switcher, "x0 y26 w" Width " h100 -HDR -Multi +LV0x10000 +LV0x4000 -E0x200 AltSubmit -TabStop", "title|exe", Switcher.ListViewAction.Bind(Switcher))
Switcher.CLV := new LV_Colors(Switcher.LV.hwnd)
Switcher.CLV.SelectionColors("0x" SelColor, "0xFFFFFF")
Switcher.IL := new Gui.ImageList(,, LargeIcons)
Switcher.LV.SetImageList(Switcher.IL.ID)
Switcher.Icons := []

; bind hotkeys
new Hotkey(Key, Switcher.Toggle.Bind(Switcher))
new Hotkey(Key1, Switcher.Toggle.Bind(Switcher))
new Hotkey(PasteKey, Switcher.PasteToggle.Bind(Switcher))
new Hotkey(PasteKey1, Switcher.PasteToggle.Bind(Switcher))
new Hotkey("Enter", Switcher.Go.Bind(Switcher), Switcher.ahkid)
new Hotkey("Delete", Switcher.Stop.Bind(Switcher), Switcher.ahkid)
new Hotkey("Up", Switcher.Move.Bind(Switcher, -1), Switcher.ahkid)
new Hotkey("Esc & k", Switcher.Move.Bind(Switcher, -1), Switcher.ahkid)
new Hotkey("^i", Switcher.Move.Bind(Switcher, -1), Switcher.ahkid)
new Hotkey("Down", Switcher.Move.Bind(Switcher, +1), Switcher.ahkid)
new Hotkey("Esc & j", Switcher.Move.Bind(Switcher, +1), Switcher.ahkid)
new Hotkey("^k", Switcher.Move.Bind(Switcher, +1), Switcher.ahkid)
new Hotkey("^Backspace", Switcher.CtrlBackspace.Bind(Switcher), Switcher.ahkid)

DllCall("RegisterShellHookWindow", "ptr", Switcher.hwnd)
OnMessage(DllCall("RegisterWindowMessage", "Str", "SHELLHOOK"), Func("RegisterWindowMessage"))
return

RegisterWindowMessage(wParam, hwnd) {
    if (wParam == 4 || wParam == 32772) {
        if (Switcher.IsVisible) {
            hwnd := WinActive()
            if (hwnd != Switcher.hwnd)
                Switcher.Close()
        }
    }
}

Command(cmd) {
    if (cmd = "reload")
        reload
    if (cmd = "Exit")
        reload
    ; ExitApp
}

pa(array, depth=5, indentLevel:=" ") { ; tidbit, this has saved my life
    try {
        for k,v in Array {
            lst.= indentLevel "[" k "]"
            if (IsObject(v) && depth>1)
                lst.="`n" pa(v, depth-1, indentLevel . " ")
            else
                lst.=" => " v
            lst.="`n"
        } return rtrim(lst, "`r`n `t")
    } return
}

m(x*) {
    for a, b in x
        text .= (IsObject(b)?pa(b):b) "`n"
    MsgBox, 0, msgbox, % text
}

LV_EX_GetRowHeight(HLV, Row := 1) {
    Return LV_EX_GetItemRect(HLV, Row).H
}

LV_EX_GetItemRect(HLV, Row := 1, LVIR := 0, Byref RECT := "") {
    ; LVM_GETITEMRECT = 0x100E -> http://msdn.microsoft.com/en-us/library/bb761049(v=vs.85).aspx
    VarSetCapacity(RECT, 16, 0)
    NumPut(LVIR, RECT, 0, "Int")
    SendMessage, 0x100E, % (Row - 1), % &RECT, , % "ahk_id " . HLV
    If (ErrorLevel = 0)
        Return False
    Result := {}
    Result.X := NumGet(RECT, 0, "Int")
    Result.Y := NumGet(RECT, 4, "Int")
    Result.R := NumGet(RECT, 8, "Int")
    Result.B := NumGet(RECT, 12, "Int")
    Result.W := Result.R - Result.X
    Result.H := Result.B - Result.Y
    Return Result
}

#Include lib\Class_LV_Colors.ahk
#Include lib\Class_Hotkey.ahk
#Include lib\Class_Switcher.ahk
#Include lib\Fuzzy.ahk
; #Include lib\colemak.ahk
; #Include lib\fastKey.ahk
#Include lib\fastKey_qwer.ahk